package icu.stopit.demo.View.Setting;

import icu.stopit.demo.Utils.Utils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class SettingView {
    private JPanel contentPane;
    private JTextField OPENAI_TOKEN;
    private JTextField USER;
    private JComboBox MODEL;
    private JTextField MAX_TOKENS;
    private JTextField TOP_P;
    private JTextField TEMPERATURE;
    private JTextField BEST_OF;
    private JComboBox SIZE;
    private JTextField N;
    private JTextField PRESENCE_PENALTY;
    private JTextField FREQUENCY_PENALTY;
    private JTextField FIXING;
    private JTextField QUESTION;

    public SettingView() {
        $$$setupUI$$$();
        readProp();
        contentPane.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                readProp();
            }

            @Override
            public void focusLost(FocusEvent e) {

            }
        });
    }

    public void readProp() {
        OPENAI_TOKEN.setText(Utils.properties.getProperty("OPENAI_TOKEN"));
        USER.setText(Utils.properties.getProperty("USER"));
        MODEL.setSelectedItem(Utils.properties.getProperty("MODEL"));
        MAX_TOKENS.setText(Utils.properties.getProperty("MAX_TOKENS"));
        TOP_P.setText(Utils.properties.getProperty("TOP_P"));
        TEMPERATURE.setText(Utils.properties.getProperty("TEMPERATURE"));
        BEST_OF.setText(Utils.properties.getProperty("BEST_OF"));
        SIZE.setSelectedItem(Utils.properties.getProperty("SIZE"));
        N.setText(Utils.properties.getProperty("N"));
        PRESENCE_PENALTY.setText(Utils.properties.getProperty("PRESENCE_PENALTY"));
        FREQUENCY_PENALTY.setText(Utils.properties.getProperty("FREQUENCY_PENALTY"));
        QUESTION.setText(Utils.properties.getProperty("QUESTION"));
        FIXING.setText(Utils.properties.getProperty("FIXING"));

    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        int x = 40;
        int y = 20;
        int w = 250;
        int h = 28;
        int i = 0;
        int j = 0;
        contentPane = new JPanel();
        contentPane.setPreferredSize(new Dimension(540, 340));
        contentPane.setLayout(null);
        final JLabel label1 = new JLabel();
        label1.setText("OPENAI_TOKEN（密钥）");
        label1.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label1);
        OPENAI_TOKEN = new JTextField();
        OPENAI_TOKEN.setColumns(30);
        OPENAI_TOKEN.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(OPENAI_TOKEN);
        final JLabel label2 = new JLabel();
        label2.setText("USER（密钥使用用户）");
        label2.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label2);
        USER = new JTextField();
        USER.setColumns(30);
        USER.setEnabled(true);
        USER.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(USER);
        final JLabel label3 = new JLabel();
        label3.setText("BEST_OF( 生成的版本数）");
        label3.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label3);
        BEST_OF = new JTextField();
        BEST_OF.setColumns(30);
        BEST_OF.setText("");
        BEST_OF.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(BEST_OF);
        final JLabel label4 = new JLabel();
        label4.setText("N（返回生成的版本数）");
        label4.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label4);
        N = new JTextField();
        N.setColumns(30);
        N.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(N);
        MODEL = new JComboBox();
        final JLabel label5 = new JLabel();
        label5.setText("MODEL（模型）");
        label5.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label5);
        MODEL.setEditable(true);
        final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel();
        defaultComboBoxModel1.addElement("文本编辑：");
        defaultComboBoxModel1.addElement("text-davinci-edit-001");
        defaultComboBoxModel1.addElement("code-davinci-edit-001");
        defaultComboBoxModel1.addElement("文本回复：");
        defaultComboBoxModel1.addElement("babbage");
        defaultComboBoxModel1.addElement("davinci");
        defaultComboBoxModel1.addElement("text-embedding-ada-002");
        defaultComboBoxModel1.addElement("babbage-code-search-code");
        defaultComboBoxModel1.addElement("text-similarity-babbage-001");
        defaultComboBoxModel1.addElement("text-davinci-001");
        defaultComboBoxModel1.addElement("curie-instruct-beta");
        defaultComboBoxModel1.addElement("babbage-code-search-text");
        defaultComboBoxModel1.addElement("babbage-similarity");
        defaultComboBoxModel1.addElement("curie-search-query");
        defaultComboBoxModel1.addElement("code-search-babbage-text-001");
        defaultComboBoxModel1.addElement("code-cushman-001");
        defaultComboBoxModel1.addElement("code-search-babbage-code-001");
        defaultComboBoxModel1.addElement("audio-transcribe-deprecated");
        defaultComboBoxModel1.addElement("text-ada-001");
        defaultComboBoxModel1.addElement("text-similarity-ada-001");
        defaultComboBoxModel1.addElement("text-davinci-insert-002");
        defaultComboBoxModel1.addElement("text-davinci-003");
        defaultComboBoxModel1.addElement("ada-code-search-code");
        defaultComboBoxModel1.addElement("ada-similarity");
        defaultComboBoxModel1.addElement("code-search-ada-text-001");
        defaultComboBoxModel1.addElement("text-search-ada-query-001");
        defaultComboBoxModel1.addElement("text-curie-001");
        defaultComboBoxModel1.addElement("text-davinci-edit-001");
        defaultComboBoxModel1.addElement("davinci-search-document");
        defaultComboBoxModel1.addElement("ada-code-search-text");
        defaultComboBoxModel1.addElement("text-search-ada-doc-001");
        defaultComboBoxModel1.addElement("code-davinci-002");
        defaultComboBoxModel1.addElement("code-davinci-edit-001");
        defaultComboBoxModel1.addElement("davinci-instruct-beta");
        defaultComboBoxModel1.addElement("text-similarity-curie-001");
        defaultComboBoxModel1.addElement("code-search-ada-code-001");
        defaultComboBoxModel1.addElement("ada-search-query");
        defaultComboBoxModel1.addElement("text-search-davinci-query-001");
        defaultComboBoxModel1.addElement("ada");
        defaultComboBoxModel1.addElement("davinci-search-query");
        defaultComboBoxModel1.addElement("text-davinci-insert-001");
        defaultComboBoxModel1.addElement("babbage-search-document");
        defaultComboBoxModel1.addElement("ada-search-document");
        defaultComboBoxModel1.addElement("text-search-babbage-doc-001");
        defaultComboBoxModel1.addElement("text-search-curie-doc-001");
        defaultComboBoxModel1.addElement("text-search-curie-query-001");
        defaultComboBoxModel1.addElement("babbage-search-query");
        defaultComboBoxModel1.addElement("text-babbage-001");
        defaultComboBoxModel1.addElement("text-search-davinci-doc-001");
        defaultComboBoxModel1.addElement("text-search-babbage-query-001");
        defaultComboBoxModel1.addElement("curie-similarity");
        defaultComboBoxModel1.addElement("curie-search-document");
        defaultComboBoxModel1.addElement("curie");
        defaultComboBoxModel1.addElement("text-similarity-davinci-001");
        defaultComboBoxModel1.addElement("text-davinci-002");
        defaultComboBoxModel1.addElement("davinci-similarity");
        defaultComboBoxModel1.addElement("cushman:2020-05-03");
        defaultComboBoxModel1.addElement("ada:2020-05-03");
        defaultComboBoxModel1.addElement("babbage:2020-05-03");
        defaultComboBoxModel1.addElement("curie:2020-05-03");
        defaultComboBoxModel1.addElement("davinci:2020-05-03");
        defaultComboBoxModel1.addElement("if-davinci-v2");
        defaultComboBoxModel1.addElement("if-curie-v2");
        defaultComboBoxModel1.addElement("if-davinci:3.0.0");
        defaultComboBoxModel1.addElement("davinci-if:3.0.0");
        defaultComboBoxModel1.addElement("davinci-instruct-beta:2.0.0");
        defaultComboBoxModel1.addElement("text-ada:001");
        defaultComboBoxModel1.addElement("text-davinci:001");
        defaultComboBoxModel1.addElement("text-curie:001");
        defaultComboBoxModel1.addElement("text-babbage:001");
        MODEL.setModel(defaultComboBoxModel1);
        MODEL.setToolTipText("文本编辑：\ntext-davinci-edit-001 \ncode-davinci-edit-001");
        MODEL.putClientProperty("html.disable", Boolean.FALSE);
        MODEL.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(MODEL);
        final JLabel label6 = new JLabel();
        label6.setText("MAX_TOKENS（最大回复字数）");
        label6.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label6);
        MAX_TOKENS = new JTextField();
        MAX_TOKENS.setColumns(30);
        MAX_TOKENS.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(MAX_TOKENS);
        final JLabel label7 = new JLabel();
        label7.setText("TEMPERATURE（随机比 0-2）");
        label7.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label7);
        TEMPERATURE = new JTextField();
        TEMPERATURE.setColumns(30);
        TEMPERATURE.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(TEMPERATURE);
        final JLabel label8 = new JLabel();
        label8.setText("TOP_P（取样比  0-1）");
        label8.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label8);
        TOP_P = new JTextField();
        TOP_P.setColumns(30);
        TOP_P.setEditable(true);
        TOP_P.setText("");
        TOP_P.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(TOP_P);
        final JLabel label9 = new JLabel();
        label9.setText("SIZE（图片大小）");
        label9.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label9);
        SIZE = new JComboBox();
        final DefaultComboBoxModel defaultComboBoxModel2 = new DefaultComboBoxModel();
        defaultComboBoxModel2.addElement("256X256");
        defaultComboBoxModel2.addElement("512X512");
        defaultComboBoxModel2.addElement("1024X1024");
        SIZE.setModel(defaultComboBoxModel2);
        SIZE.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(SIZE);


        final JLabel label22 = new JLabel();
        label22.setText("PRESENCE_PENALTY（创新加分  -2-2）");
        label22.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label22);
        PRESENCE_PENALTY = new JTextField();
        PRESENCE_PENALTY.setColumns(30);
        PRESENCE_PENALTY.setEditable(true);
        PRESENCE_PENALTY.setText("");
        PRESENCE_PENALTY.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(PRESENCE_PENALTY);

        final JLabel label23 = new JLabel();
        label23.setText("FREQUENCY_PENALTY（非重复加分 -2-2）");
        label23.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label23);
        FREQUENCY_PENALTY = new JTextField();
        FREQUENCY_PENALTY.setColumns(30);
        FREQUENCY_PENALTY.setEditable(true);
        FREQUENCY_PENALTY.setText("");
        FREQUENCY_PENALTY.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(FREQUENCY_PENALTY);


        final JLabel label24 = new JLabel();
        label24.setText("解释代码前缀");
        label24.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label24);
        QUESTION = new JTextField();
        QUESTION.setColumns(30);
        QUESTION.setEditable(true);
        QUESTION.setText("");
        QUESTION.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(QUESTION);
        final JLabel label25 = new JLabel();
        label25.setText("修复代码前缀");
        label25.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(label25);
        FIXING = new JTextField();
        FIXING.setColumns(30);
        FIXING.setEditable(true);
        FIXING.setText("");
        FIXING.setBounds(x + (w * ((j++) & 1)), y + (i++ / 2) * h, w, h);
        contentPane.add(FIXING);

        label1.setLabelFor(BEST_OF);
        label2.setLabelFor(USER);
        label3.setLabelFor(OPENAI_TOKEN);
        label4.setLabelFor(N);
        label5.setLabelFor(MODEL);
        label6.setLabelFor(MAX_TOKENS);
        label7.setLabelFor(TEMPERATURE);
        label8.setLabelFor(TOP_P);
        label9.setLabelFor(SIZE);
        label22.setLabelFor(PRESENCE_PENALTY);
        label23.setLabelFor(FREQUENCY_PENALTY);
        label24.setLabelFor(QUESTION);
        label25.setLabelFor(FIXING);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return contentPane;
    }

    private void createUIComponents() {
        contentPane = new JPanel();
        contentPane.setLayout(new GridLayout(9, 2));
    }

    public void save() {
        Utils.properties.setProperty("OPENAI_TOKEN", OPENAI_TOKEN.getText());
        Utils.properties.setProperty("USER", USER.getText());
        Utils.properties.setProperty("MODEL", (String) MODEL.getSelectedItem());
        Utils.properties.setProperty("MAX_TOKENS", MAX_TOKENS.getText());
        Utils.properties.setProperty("TOP_P", TOP_P.getText());
        Utils.properties.setProperty("TEMPERATURE", TEMPERATURE.getText());
        Utils.properties.setProperty("BEST_OF", BEST_OF.getText());
        Utils.properties.setProperty("SIZE", (String) SIZE.getSelectedItem());
        Utils.properties.setProperty("N", N.getText());
        Utils.properties.setProperty("PRESENCE_PENALTY", PRESENCE_PENALTY.getText());
        Utils.properties.setProperty("FREQUENCY_PENALTY", FREQUENCY_PENALTY.getText());
        Utils.properties.setProperty("FIXING", FIXING.getText());
        Utils.properties.setProperty("QUESTION", QUESTION.getText());

        Utils.save();
    }
}
